﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace PlistParser
{
    class ReadPlist
    {
        public List<DataType> DateList { get; set; }
        int saveid = 1;
        DataType RootNode;
        int count;
        int itemCount;
        
        public ReadPlist()
        {
            count = 1;
            itemCount = 0;
            RootNode = new DataType();
            RootNode.ID = count;
            RootNode.DataName = null;
            RootNode.parentID = 0;
            RootNode.ValueType = EnumValueType.DICT;
            RootNode.childrenID = new List<int>();
            this.DateList = new List<DataType>();
            DateList.Add(RootNode);
            
        }
        private void print()
        {
            foreach (var item in this.DateList)
            {
                Console.WriteLine("DataName:{0}", item.DataName);
                Console.WriteLine("ValueType:{0}", item.ValueType);
                Console.WriteLine("Value:{0}", item.Value);
                Console.WriteLine("ID:{0}", item.ID);
                Console.WriteLine("parentID:{0}", item.parentID);
                Console.Write("childrenID:");
                foreach (var c in item.childrenID)
                {
                    Console.Write("{0}  ", c.ToString());
                }
                Console.WriteLine("\n");
            }
        }
        public XDocument LoadFromFile(string path)
        {
            return XDocument.Load(path);
        }
        public void XMLparser(string path)
        {
            XDocument doc = LoadFromFile(path);
            XElement FirstElement = doc.Root.Element("dict");
            DateList[0].childrenID = XMLOnce(FirstElement, 1);
            foreach (var item in DateList)
            {
                if (item.Value == "FALSE"||item.Value == "TRUE")
                {
                    item.Value = item.Value.ToLower();
                }
                try
                {
                    if (Char.IsNumber(item.DataName[0]))
                    {
                        item.DataName.Insert(0, "_");
                    }
                }
                catch (System.Exception ex)
                {
                	
                }
                
            }
            print();
        }

        public List<int> XMLOnce(XElement nowElement,int parentid)
        {
            List<DataType> DataTemp = new List<DataType>();
            List<int> IDList = new List<int>();
            List<int> childrenIDList = new List<int>();
            var keys = from k in nowElement.Elements("key")
                      select k;
            var values = from v in nowElement.Elements()
                         where v.Name != "key"
                         select v ;
            for (int i = 0; i < values.ToList().Count; i++)
            {
                int id = ++count;
                EnumValueType valuetype = (EnumValueType)Enum.Parse(typeof(EnumValueType), values.ToList()[i].Name.LocalName.ToString().ToUpper(), true);
                string value = null;
                if (valuetype == EnumValueType.ARRAY)
                {
                    XElement newElement = nowElement.Elements().Except(nowElement.Elements("key")).ElementAt(i);
                    int num = newElement.Elements().Count();
                    for (int j = 0; j < num;j++ )
                    {
                        newElement.AddFirst(new XElement("key", "item"));
                    }
                    childrenIDList = XMLOnce(newElement,id);
                }
                else if (valuetype == EnumValueType.DICT)
                {
                    XElement newElement = nowElement.Elements().Except(nowElement.Elements("key")).ElementAt(i);
                    childrenIDList = XMLOnce(newElement, id);
                }
                else
                {
                    value = values.ToList()[i].Value.ToString();
                }
                
                try
                {
                    DataTemp.Add(new DataType()
                    {
                        DataName = keys.ToList()[i].Value.ToString(),
                        ValueType = valuetype,
                        ID = id,
                        Value = value,
                        parentID = parentid,
                        childrenID = childrenIDList
                    });
                }
                catch (System.Exception ex)
                {
                    DataTemp.Add(new DataType()
                    {
                        DataName = "itemContent",
                        ValueType = valuetype,
                        ID = id,
                        Value = value,
                        parentID = parentid,
                        childrenID = childrenIDList
                    });
                }

            }
            foreach (var item in DataTemp)
            {
                IDList.Add(item.ID);
            }
            DateList.AddRange(DataTemp);
            //count += DataTemp.Count;
            return IDList;
        }
    }
}
